package com.eascs.ticket.ticket.controller;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.sql.ObjectSqlBuilder;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.ticket.form.UserTicketStatisticsConstant;
import com.eascs.ticket.ticket.form.UserTicketStatisticsForm;
import com.eascs.ticket.ticket.service.TicketStatisticsService;
import com.eascs.web.context.RequestContext;

@Controller
public class TicketStatisticsViewController extends BaseMasterViewController {
	@Inject
	private TicketStatisticsService ticketStatisticsService;

	@RequestMapping("/ticket/statistics/view/overtime")
	public String statOvertime() {
		PaginationInfo paginationInfo = new PaginationInfo(ticketStatisticsService.statOvertime(), new String[] { "工号", "姓名", "工时（小时）" });
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(0);
		ViewContext.put("paginationInfo", paginationInfo);
		return "/ticket/view/mywatcher";

	}

	@RequestMapping("/ticket/statistics/view/tickets")
	public String statisticsUserTicket(UserTicketStatisticsForm userTicketStatisticsForm) {

		if (UserTicketStatisticsConstant.TYPE_THIS_WEEK.equals(userTicketStatisticsForm.getType())) {
			userTicketStatisticsForm.setFromDate(DateUtils.getMondayOfThisWeek());
			userTicketStatisticsForm.setToDate(DateUtils.addDate(DateUtils.getMondayOfThisWeek(), 7));
		} else if (UserTicketStatisticsConstant.TYPE_LAST_WEEK.equals(userTicketStatisticsForm.getType())) {
			userTicketStatisticsForm.setFromDate(DateUtils.addDate(DateUtils.getMondayOfThisWeek(), -7));
			userTicketStatisticsForm.setToDate(DateUtils.getMondayOfThisWeek());

		} else if (UserTicketStatisticsConstant.TYPE_THIS_MONTH.equals(userTicketStatisticsForm.getType())) {
			userTicketStatisticsForm.setFromDate(DateUtils.getFirstDayOfMonth(new Date()));
			userTicketStatisticsForm.setToDate(DateUtils.addMonth(DateUtils.getFirstDayOfMonth(new Date()), 1));
		} else if (UserTicketStatisticsConstant.TYPE_LAST_MONTH.equals(userTicketStatisticsForm.getType())) {
			userTicketStatisticsForm.setFromDate(DateUtils.addMonth(DateUtils.getFirstDayOfMonth(new Date()), -1));
			userTicketStatisticsForm.setToDate(DateUtils.getFirstDayOfMonth(new Date()));
		}

		ObjectSqlBuilder objectSqlBuilder = ObjectSqlBuilder.createWithOrderSql(userTicketStatisticsForm,
				"SELECT DISTINCT tscl.creator,tscl.ticket_id AS ticketId FROM ticket_status_change_log tscl INNER JOIN ticket t ON t.id=tscl.ticket_id INNER JOIN USER u ON u.code=tscl.creator WHERE tscl.status_from NOT IN('l6uqg12t9fiva') AND t.ticket_type_code NOT IN ('leave','overtime') AND tscl.creator IN ( SELECT us.user_code FROM user_structure us)",
				" order by tscl.creator ");
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 100, 0);
		sqlPageQuery.setQuerySql(objectSqlBuilder.getSql());
		sqlPageQuery.addParams(objectSqlBuilder.getParams());
		PaginationInfo paginationInfo = new PaginationInfo(ticketStatisticsService.statisticsUserTicket(sqlPageQuery), new String[] { "人员", "工单" });
		ViewContext.put("paginationInfo", paginationInfo);
		paginationInfo.setUrl(UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", ""));
		paginationInfo.setSelectDataOptions(new SelectDataOption[] { new SelectDataOption("allUser", "user") });
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setBuildPagingNavigation(true);
		if (StringUtil.hasText(userTicketStatisticsForm.getGroupBy())) {
			paginationInfo.setBuildPagingNavigation(false);
			paginationInfo.setTitles(new String[] { "人员", "工单数" });
			ViewContext.put("detailUrl", UrlUtil.removeParam(RequestContext.getRequestUrlWithQueryString(), "groupBy", "pageIndex"));
			return "/ticket/view/statisticTicketsWithGroup";
		}

		return "/ticket/view/mywatcher";
	}

}
